package com.ubs.training.server.service;

import com.ubs.training.server.dao.EmployeeDao;
import com.ubs.training.server.dao.UBSContactDao;
import com.ubs.training.server.dao.model.Employee;
import com.ubs.training.server.dao.model.UBSContact;
import com.ubs.training.server.dao.model.UbsIdWithFirstLastNameEmployee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/22/12
 * Time: 3:45 AM
 * To change this template use File | Settings | File Templates.
 */
@Service
public class UBSContactService implements IUBSContactService{

    @Resource (name="ubsDao")
    UBSContactDao dao;
    @Autowired
    EmployeeService emplServ;

    public EmployeeService getEmplServ() {
        return emplServ;
    }

    public void setEmplServ(EmployeeService emplServ) {
        this.emplServ = emplServ;
    }

    public UBSContactDao getDao() {
        return dao;
    }

    public void setDao(UBSContactDao dao) {
        this.dao = dao;
    }

    @Override
    public List<UBSContact> getAllUBSContact() {
        return dao.getAll();
    }

    @Override
    public UBSContact getUBSContactById(int id) {
        return dao.getById(id);
    }

    @Override
    public void saveUBSContact(UBSContact u) {
        if(u.getId() == 0){
            //svc.logEvent("User adding new UBS contact - " + u.getId());
            dao.add(u);
        }else{
            //svc.logEvent("User update UBS contact - " + u.getId());
            dao.update(u);
        }
    }

    @Override
    public void deleteUBSContact(int id) {
        //svc.logEvent("User deleting UBS contact - " + id);
        dao.delete(id);
    }

    @Override
    public List<UBSContact> getAllUBSContactByClientId(int id) {
        //svc.logEvent("User get list UBS contact from client- " + id);
        return dao.getAllUBSContactByClientId(id);
    }

    public Employee getEmployeeFromListById(List <Employee> emplList, int id){
        //List <Employee> emplList = emplServ.getAll();
        for(Employee e: emplList){
            if(e.getId() == id){
                return e;
            }
        }
        return null;
    }

    public List<UbsIdWithFirstLastNameEmployee> getIdWithFirstLastName(int id){
        //svc.logEvent("User get UBS contact from client- " + id);
        List<UBSContact> ubs = dao.getAllUBSContactByClientId(id);
        List<UbsIdWithFirstLastNameEmployee> list = new ArrayList<UbsIdWithFirstLastNameEmployee>();
        List <Employee> emplList = emplServ.getAllEmployee();
        Employee employee;
        //svc.logEvent("UBSContact Empl size " + ubs.size());
        for(UBSContact u : ubs){
            //svc.logEvent("UBSContact Empl id " + u.getEmp());
            employee = getEmployeeFromListById(emplList, u.getEmp());
            //employee = emplServ.getById(u.getEmp());
            list.add(new UbsIdWithFirstLastNameEmployee(u.getId(),employee.getFname(),employee.getLname()));
        }
        return list;
    }


}
